package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.domain.po.Task;
import com.heima.domain.po.User;
import com.heima.domain.vo.TaskVo;
import com.heima.mapper.TaskMapper;
import com.heima.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Autowired
    private UserServiceImpl userService;
    /**
     * 获取工单详情
     *
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<TaskVo> getTaskReport(String start, String end) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startTime = LocalDateTime.parse(start, formatter);
        LocalDateTime endTime = LocalDateTime.parse(end, formatter);
        List<TaskVo> list = new ArrayList<>();
        list.add(getTaskVo(2,false,startTime,endTime));
        list.add(getTaskVo(1,true,startTime,endTime));
        return list;
    }

    private TaskVo getTaskVo(int productTypeId, boolean flag, LocalDateTime startTime, LocalDateTime endTime) {
        TaskVo taskVo = new TaskVo();
        taskVo.setTotal(lambdaQuery().between(Task::getUpdateTime, startTime, endTime).eq(Task::getProductTypeId, 2).count());
        taskVo.setCompletedTotal(lambdaQuery().between(Task::getUpdateTime, startTime, endTime)
                .eq(Task::getProductTypeId, productTypeId)
                .eq(Task::getTaskStatus,4)
                .count());
        taskVo.setProgressTotal(lambdaQuery().between(Task::getUpdateTime, startTime, endTime)
                .eq(Task::getProductTypeId, productTypeId)
                .eq(Task::getTaskStatus,2)
                .count());
        taskVo.setCancelTotal(lambdaQuery().between(Task::getUpdateTime, startTime, endTime)
                .eq(Task::getProductTypeId, productTypeId)
                .eq(Task::getTaskStatus,3)
                .count());
        taskVo.setWorkerCount(userService.lambdaQuery().eq(User::getStatus,1).count());
        taskVo.setRepair(flag);
        return taskVo;
    }
}
